package com.ibeeking.found.msg.rest.service.impl;

import com.ibeeking.found.msg.rest.service.IWxMpMessageReturnService;
import com.ibeeking.found.msg.service.common.dos.WxMpMessageReturnDO;
import com.ibeeking.found.msg.service.common.query.WxMpMessageReturnPageQuery;
import com.ibeeking.found.msg.service.common.vo.WxMpMessageReturnVO;
import com.ibeeking.found.msg.service.mapper.WxmpMessageReturnMapper;
import com.ibeeking.nematos.utils.bean.BeanUtil;

import com.alibaba.nacos.common.utils.IoUtils;
import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.chanjar.weixin.common.util.XmlUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

@Deprecated
@Service
public class WxMpMessageReturnServiceImpl extends ServiceImpl<WxmpMessageReturnMapper, WxMpMessageReturnDO> implements IWxMpMessageReturnService {

    @Resource
    private WxmpMessageReturnMapper wxmpMessageReturnMapper;

    /**
     * 从微信远端接收回复事件
     *
     * @param file
     * @return
     */
    @Override
    public Object messageCallBack(MultipartFile file) {
        Map<String, Object> map = null;
        try {
            map = XmlUtils.xml2Map(IoUtils.toString(file.getInputStream(), StandardCharsets.UTF_8.toString()));
        }catch (Exception e){
            e.printStackTrace();
        }
        WxMpMessageReturnDO wxmpMessageReturnDO = this.getWxmpMessageRturnD0(map);
        this.wxmpMessageReturnMapper.insert(wxmpMessageReturnDO);
        return "success";
    }

    /**
     * 分页查询
     *
     * @param basePage
     * @return
     */
    @Override
    public Page<WxMpMessageReturnVO> pageList(WxMpMessageReturnPageQuery basePage) {
        QueryWrapper<WxMpMessageReturnDO> queryWrapper = new QueryWrapper<>();
        Page<WxMpMessageReturnDO> page = new Page(basePage.getPageNum(),basePage.getPageSize());
        queryWrapper.lambda().orderByDesc(WxMpMessageReturnDO::getWxMpCreateTime);
        queryWrapper.lambda().eq(StringUtils.isNotBlank(basePage.getMsgType()), WxMpMessageReturnDO::getMsgType,basePage.getMsgType());
        page = this.wxmpMessageReturnMapper.selectPage(page,queryWrapper);
       return BeanUtil.convertPage(page, WxMpMessageReturnVO.class);
    }

    private WxMpMessageReturnDO getWxmpMessageRturnD0(Map<String, Object> map){
        WxMpMessageReturnDO message = new WxMpMessageReturnDO();
        message.setToUserName(String.valueOf(map.get("ToUserName")));
        message.setFromUserName(String.valueOf(map.get("FromUserName")));
        message.setWxMpCreateTime(Long.valueOf(String.valueOf(map.get("CreateTime"))));
        String msgType = String.valueOf(map.get("MsgType"));
        message.setMsgType(msgType);
        switch (msgType){
            case "text":
                text(map,message);
                return  message;
            case "image":
                image(map,message);
                return  message;
            case "voice":
                voice(map,message);
                return  message;
            case "music":
                music(map,message);
                return  message;
            case "video":
                video(map,message);
                return  message;
            case "news":
                news(map,message);
                return  message;
            default:
                return message;
        }

    }

    /**
     * 文本类型
     * @param map
     * @param message
     */
    private void text(Map<String, Object> map , WxMpMessageReturnDO message){
        message.setContent(String.valueOf(map.get("Content")));
    }

    /**
     *
     * @param map
     * @param message
     */
    private void image(Map<String, Object> map , WxMpMessageReturnDO message){
        Map<String, Object> image = (Map<String, Object>) map.get("Image");
        message.setMediaId(String.valueOf(image.get("MediaId")));
    }

    private void voice(Map<String, Object> map , WxMpMessageReturnDO message){
        Map<String, Object> voice = (Map<String, Object>) map.get("Voice");
        message.setMediaId(String.valueOf(voice.get("MediaId")));
    }

    private void music(Map<String, Object> map , WxMpMessageReturnDO message){
        Map<String, Object> music = (Map<String, Object>) map.get("Music");
        message.setTitle(String.valueOf(music.get("Title")));
        message.setDescription(String.valueOf(music.get("Description")));
        message.setMusicUrl(String.valueOf(music.get("MusicUrl")));
        message.setHqMusicUrl(String.valueOf(music.get("HQMusicUrl")));
        message.setThumbMediaId(String.valueOf(music.get("ThumbMediaId")));
    }

    private void video(Map<String, Object> map , WxMpMessageReturnDO message){

        Map<String, Object> music = (Map<String, Object>) map.get("Music");
        message.setMediaId(String.valueOf(music.get("MediaId")));
        message.setTitle(String.valueOf(music.get("Title")));
        message.setDescription(String.valueOf(music.get("Description")));

    }

    private void news(Map<String, Object> map , WxMpMessageReturnDO message){

        List<Map<String, Object>> articles = (List<Map<String, Object>>) map.get("Articles");
        for (Map<String, Object> item : articles ){
            message.setTitle(String.valueOf(item.get("Title")));
            message.setDescription(String.valueOf(item.get("Description")));
            message.setPicUrl(String.valueOf(item.get("PicUrl")));
            message.setUrl(String.valueOf(item.get("Url")));
        }


    }

}
